package com.zw.yzk.kabu.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.TextView;

import com.hhl.library.FlowTagLayout;
import com.hhl.library.OnTagSelectListener;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.adapter.flowtag.GoodsTagAdapter;
import com.zw.yzk.kabu.model.GoodsDetailsModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhanwei on 2017/2/6.
 */

public class GoodsPropertyAdapter extends RecyclerView.Adapter<GoodsPropertyAdapter.Holder> {

    private List<GoodsDetailsModel.Property> dataList;
    private Context context;

    public GoodsPropertyAdapter(List<GoodsDetailsModel.Property> list) {
        dataList = new ArrayList<>();
        if (list != null) {
            this.dataList.addAll(list);
        }
        for (GoodsDetailsModel.Property property : dataList) {
            if (!property.getModelList().isEmpty()) {
                for (GoodsDetailsModel.Model model : property.getModelList()) {
                    if (model.isSelected()) {
                        model.setSelected(false);
                    }
                }
                property.getModelList().get(0).setSelected(true);
            }
        }
    }

    public List<GoodsDetailsModel.Property> getDataList() {
        return dataList;
    }

    public String getSkuProperty() {
        StringBuilder builder = new StringBuilder();
        for (GoodsDetailsModel.Property property : dataList) {
            builder.append("_").append(String.valueOf(property.getPropertyId()));
            for (GoodsDetailsModel.Model model : property.getModelList()) {
                if (model.isSelected()) {
                    builder.append(":").append(String.valueOf(model.getId()));
                }
            }
        }
        return builder.toString();
    }

    public String getSkuContent() {
        StringBuilder builder = new StringBuilder();
        for (GoodsDetailsModel.Property property : dataList) {
            builder.append("[").append(property.getContent());
            for (GoodsDetailsModel.Model model : property.getModelList()) {
                if (model.isSelected()) {
                    builder.append(" ").append(model.getContent());
                }
            }
            builder.append("]");
        }
        return builder.toString();
    }

    public String getContent() {
        StringBuilder builder = new StringBuilder();
        for (GoodsDetailsModel.Property property : dataList) {
            for (GoodsDetailsModel.Model model : property.getModelList()) {
                if (model.isSelected()) {
                    builder.append(" ").append(model.getContent());
                }
            }
        }
        return builder.toString();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null) {
            context = parent.getContext();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_item_goods_property_list, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(final Holder holder, int position) {
        GoodsDetailsModel.Property model = dataList.get(position);
        holder.type.setText(model.getContent());
        holder.setTagAdapter(model.getModelList(), new OnTagSelectListener() {
            @Override
            public void onItemSelect(FlowTagLayout parent, List<Integer> selectedList) {
                int position = holder.getAdapterPosition();
                int size = dataList.get(position).getModelList().size();
                for (int i = 0; i < size; i++) {
                    dataList.get(position).getModelList().get(i).setSelected(false);
                }
                dataList.get(position).getModelList().get(selectedList.get(0)).setSelected(true);

                if (listener != null) {
                    listener.onSelected();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public static class Holder extends RecyclerView.ViewHolder {
        @Bind(R.id.type)
        TextView type;
        @Bind(R.id.child_type)
        FlowTagLayout tag;

        private GoodsTagAdapter adapter;
        public OnTagSelectListener listener;

        public void setTagAdapter(List<GoodsDetailsModel.Model> list, OnTagSelectListener listener) {
            if (adapter == null) {
                tag.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_SINGLE);
                adapter = new GoodsTagAdapter(list);
                tag.setAdapter(adapter);
                tag.setOnTagSelectListener(listener);
            } else {
                adapter.refresh(list);
            }
        }

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnPropertySelectedListener {
        void onSelected();
    }

    private OnPropertySelectedListener listener;

    public void setOnPropertySelectedListener(OnPropertySelectedListener listener) {
        this.listener = listener;
    }
}
